##!/bin/bash



## creat couchdb by doocker 
docker-compose -f docker-compose-couchdb.yaml up -d

## sleep 5
echo "sleep 5 s"
sleep 5
echo ""

## enable cluster
echo "enable cluster ..."
curl -X POST -H "Content-Type: application/json"  http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address": "0.0.0.0", "username": "admin", "password": "password", "node_count": "3"}'
curl -X POST -H "Content-Type: application/json"  http://admin:password@127.0.0.1:6984/_cluster_setup -d '{"action": "enable_cluster", "bind_address": "0.0.0.0", "username": "admin", "password": "password", "node_count": "3"}'
curl -X POST -H "Content-Type: application/json"  http://admin:password@127.0.0.1:7984/_cluster_setup -d '{"action": "enable_cluster", "bind_address": "0.0.0.0", "username": "admin", "password": "password", "node_count": "3"}'


## cluster 1
echo "set cluster 1 ..."
curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address": "0.0.0.0", "username": "admin", "password": "password", "port": 5984, "node_count": "3", "remote_node": "couchdb.two", "remote_current_user": "admin", "remote_current_password": "admin"}'
curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "add_node", "host": "couchdb.two", "port": 5984, "username": "admin", "password": "password"}'

## cluster 2
echo "set cluster 2 ..."
curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address": "0.0.0.0", "username": "admin", "password": "password", "port": 5984, "node_count": "3", "remote_node": "couchdb.three", "remote_current_user": "admin", "remote_current_password": "admin"}'
curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "add_node", "host": "couchdb.three", "port": 5984, "username": "admin", "password": "password"}'

## finish_cluster
echo "finish_cluster ..."
curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "finish_cluster"}'


## query cluster
echo "query couchdb cluster"
curl -X GET "http://admin:password@localhost:5984/_membership"




